package life.ks98.community.controller;

import life.ks98.community.dto.PaginationDTO;
import life.ks98.community.model.User;
import life.ks98.community.service.NotificationService;
import life.ks98.community.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * @author Kyrie
 * @date 2021/2/11 - 21:15
 */
@Controller
public class ProfileController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private NotificationService notificationService;

    @GetMapping("/profile/{action}")
    public String profile(Model model,
                          HttpServletRequest request,
                          @PathVariable("action") String action,
                          @RequestParam(name = "page", defaultValue = "1") Integer page,
                          @RequestParam(name = "size", defaultValue = "8") Integer size) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/";
        }
        PaginationDTO pagination = null;
        if (StringUtils.equals("questions", action)) {
            pagination = questionService.list(user.getId(), page, size);
            model.addAttribute("section", "questions");
            model.addAttribute("pagination", pagination);
            model.addAttribute("sectionName", "我的提问");
        } else if (StringUtils.equals("replies", action)) {
            PaginationDTO paginationDTO = notificationService.list(user.getId(), page, size);
            model.addAttribute("section", "replies");
            model.addAttribute("pagination", paginationDTO);
            model.addAttribute("sectionName", "最新回复");
        }
        return "profile";
    }
}
